#include "Render.h"

Render::Render(shared_ptr<BitmapDraw > bitmaps, ALLEGRO_DISPLAY* display):

    _bitmaps(bitmaps),
    _face_direction(RIGHT),
    _ID(0),
    _display(display),
    _ArrowImage(bitmaps->get_ArrowImage()),
    _MainImage(bitmaps->get_MainImage()),
    _TempImage(bitmaps->get_RedMonsterImage()),
    _arrowExpand(0)
{}

void Render::initialise()
{
    _temp_pos_x = 650;
    _temp_pos_y = 350;
}

void Render::render_Soil()
{
    al_set_target_bitmap(_bitmaps->get_SoilImage());

    al_hold_bitmap_drawing(true);
    al_draw_filled_rectangle(_temp_pos_x, _temp_pos_y-100, _temp_pos_x+50, _temp_pos_y-50, al_map_rgb(0,0,0));
    al_hold_bitmap_drawing(false);

    al_set_target_bitmap(_MainImage);

    al_hold_bitmap_drawing(true);
    al_draw_bitmap(_bitmaps->get_SoilImage(),0,100,0);
    al_draw_filled_rectangle(0, 0, 1300, 100, al_map_rgb(0,0,0));
    al_hold_bitmap_drawing(false);

}

void Render::render_Man(shared_ptr<Man > DigDug)
{
    _face_direction = DigDug -> get_face_direction();

    _temp_pos_x = DigDug -> get_pos_x();
    _temp_pos_y = DigDug -> get_pos_y();


    if ( _face_direction == RIGHT )  //face right
    {
        al_draw_bitmap(_bitmaps->get_DigDugImage(), _temp_pos_x, _temp_pos_y, 0);
        _face_direction = RIGHT;
    }

    else if ( _face_direction == LEFT ) //face left
    {
        al_draw_bitmap(_bitmaps->get_DigDugImage(), _temp_pos_x, _temp_pos_y, ALLEGRO_FLIP_HORIZONTAL);
        _face_direction = LEFT;
    }

    else if ( _face_direction == UP )  //face up
    {
        al_draw_rotated_bitmap(_bitmaps->get_DigDugImage(), 50, 0, _temp_pos_x, _temp_pos_y, 3*ALLEGRO_PI/2, 0 );
        _face_direction = UP;
    }

    else if ( _face_direction == DOWN ) //face down
    {
        al_draw_rotated_bitmap(_bitmaps->get_DigDugImage(), 0, 50, _temp_pos_x, _temp_pos_y, ALLEGRO_PI/2, 0 );
        _face_direction = DOWN;
    }

}

void Render::render_Arrow(shared_ptr<Arrow > arrow)
{
    update_ArrowBitmap(arrow);

    al_set_target_bitmap(_MainImage);

    al_hold_bitmap_drawing(true);

    if ( _face_direction == RIGHT )
    {
        al_draw_bitmap(_ArrowImage, _temp_pos_x+50, _temp_pos_y, 0);
    }
    else if ( _face_direction == LEFT )
    {
        al_draw_bitmap(_ArrowImage, _temp_pos_x-150, _temp_pos_y, ALLEGRO_FLIP_HORIZONTAL);
    }

    else if ( _face_direction == UP )
    {
        al_draw_rotated_bitmap(_ArrowImage, 50, 0, _temp_pos_x, _temp_pos_y-50, 3*ALLEGRO_PI/2 , 0);
    }

    else if ( _face_direction == DOWN )
    {
        al_draw_rotated_bitmap(_ArrowImage, 0, 50, _temp_pos_x, _temp_pos_y+50, ALLEGRO_PI/2 , 0);
    }

    al_hold_bitmap_drawing(false);
}

void Render::update_ArrowBitmap(shared_ptr<Arrow > arrow)
{

        al_set_target_bitmap(_bitmaps->get_ArrowImage());
        al_clear_to_color(al_map_rgb(0,0,0));
        al_convert_mask_to_alpha(_bitmaps->get_ArrowImage(), al_map_rgb(0,0,0));

    if (!arrow->get_invisible())
    {
        _arrowExpand = arrow->get_arrowExpand();

        al_hold_bitmap_drawing(true);

        al_draw_line(0,36,1*_arrowExpand,24+_arrowExpand,al_map_rgb(255,255,255),2);
        al_draw_line(1*_arrowExpand,24+_arrowExpand,2*_arrowExpand,48-_arrowExpand,al_map_rgb(255,255,255),2);     //arm
        al_draw_line(2*_arrowExpand,48-_arrowExpand,3*_arrowExpand,24+_arrowExpand,al_map_rgb(255,255,255),2);
        al_draw_line(3*_arrowExpand,24+_arrowExpand,4*_arrowExpand,48-_arrowExpand,al_map_rgb(255,255,255),2);
        al_draw_line(4*_arrowExpand,48-_arrowExpand,5*_arrowExpand,24+_arrowExpand,al_map_rgb(255,255,255),2);
        al_draw_line(5*_arrowExpand,24+_arrowExpand,6*_arrowExpand,48-_arrowExpand,al_map_rgb(255,255,255),2);     //arm
        al_draw_line(6*_arrowExpand,48-_arrowExpand,7*_arrowExpand,24+_arrowExpand,al_map_rgb(255,255,255),2);
        al_draw_line(7*_arrowExpand,24+_arrowExpand,8*_arrowExpand,48-_arrowExpand,al_map_rgb(255,255,255),2);
        al_draw_line(8*_arrowExpand,48-_arrowExpand,9*_arrowExpand,36,al_map_rgb(255,255,255),2);
        al_draw_line(9*_arrowExpand,36,10*_arrowExpand,36,al_map_rgb(0,0,150),2);

        al_draw_line(9*_arrowExpand,32,10*_arrowExpand,36,al_map_rgb(0,0,255),2);    //head
        al_draw_line(9*_arrowExpand,40,10*_arrowExpand,36,al_map_rgb(0,0,255),2);

        al_hold_bitmap_drawing(false);

    }

}

void Render::render_Monster(int face_direction, float pos_x, float pos_y, bool disembody)
{
    if (disembody) _TempImage = _bitmaps->get_DisembodiedRedMonsterImage();
    else _TempImage = _bitmaps->get_RedMonsterImage();

    al_hold_bitmap_drawing(true);

    if ( face_direction == RIGHT )  //face right
    {
        al_draw_bitmap(_TempImage, pos_x, pos_y, 0);
    }

    else if ( face_direction == LEFT ) //face left
    {
        al_draw_bitmap(_TempImage, pos_x, pos_y, ALLEGRO_FLIP_HORIZONTAL);
    }

    else if ( face_direction == UP )  //face up
    {
        al_draw_rotated_bitmap(_TempImage, 50, 0, pos_x, pos_y, 3*ALLEGRO_PI/2, 0 );
    }

    else if ( face_direction == DOWN ) //face down
    {
        al_draw_rotated_bitmap(_TempImage, 0, 50, pos_x, pos_y, ALLEGRO_PI/2, 0 );
    }

    al_hold_bitmap_drawing(false);
}

void Render::render_Title()
{
    al_set_target_backbuffer(_display);
    al_draw_bitmap(_bitmaps -> get_TitleImage(), 0, 0, 0);
}

void Render::render_LevelPass()
{
    al_set_target_backbuffer(_display);
    al_draw_bitmap(_bitmaps -> get_LevelPassImage(), 0, 0, 0);
}

void Render::render_GameOver()
{
    al_set_target_backbuffer(_display);
    al_draw_bitmap(_bitmaps -> get_GameOverImage(), 0, 0, 0);
}

ALLEGRO_BITMAP* Render::get_MainImage()
{
    return _MainImage;
}

